<!DOCTYPE html>
<html>
<body>
    <pre id="console"></pre>
    <iframe id="aFrame"></iframe>
    <script>
        function log(msg)
        {
            document.getElementById('console').appendChild(document.createTextNode(msg + '\n'));
        }
    
        if (window.testRunner) {
            testRunner.dumpAsText();
            testRunner.waitUntilDone();
        }

        var targetWindow = frames[0];
        if (!targetWindow.document)
            log("FAIL: targetWindow started with no document, we won't know if the test passed or failed.");

        var selection = targetWindow.getSelection();

        var iframe = document.getElementById('aFrame');
        targetWindow.location = "http://localhost:8000/security/resources/foreign-domain-iframe-for-selection-test.html";

        setTimeout(test, 1);

        setTimeout(function() {
            log("FAIL: Tested timed-out.");
            if (window.testRunner)
                testRunner.notifyDone();
        }, 5000);

        function test() {
            try {
                if (targetWindow.document) {
                    setTimeout(test, 1);
                    return;
                }
            } catch (e) {
                log("Exception thrown trying to get cross-domain document: " + e);
            }

            try {
                var newDoc = selection.anchorNode.ownerDocument;
                if (newDoc.baseURI == "http://localhost:8000/security/resources/foreign-domain-iframe-for-selection-test.html")
                    log("FAIL: Cross-site access allowed.");
            } catch (e) {
                log("PASS: Cross-site access denied.");
            }
            if (window.testRunner)
                testRunner.notifyDone();
        }
    </script>
</body>
</html>
